# -*- coding: utf-8 -*-

from odoo import models, fields, api


class HrAttendanceMonthReport(models.Model):
    _inherit = 'hr.attendance.month.report'
    _description = 'hr_attendance_inherit_gh.hr_attendance_inherit_gh'
    """
    进厂日期	应出勤天数	平时加班时数	周末加班时数	加班总时数	迟到（分钟）	早退（分钟）	矿工（天）	补卡次数	平时请假（H）	周末请假（H）	奖惩

    """

    date_entry=fields.Date(string=u'进厂日期',related='employee_id.date_entry',store=True)
    barcode=fields.Char(related='employee_id.barcode',string=u'工号',store=True)
    production_line_id = fields.Many2one('hr.production.line', string='线别',related='employee_id.production_line_id',store=True)
    total_day=fields.Integer(string=u'应出勤天数')
    attendance_1=fields.Integer(string='平時出勤')
    attendance_2 = fields.Integer(string='周末出勤')
    overtime_1=fields.Float(string=u'平时加班时数')
    overtime_2 = fields.Float(string=u'周末加班时数')
    overtime_all=fields.Float(string=u'加班总时数')
    buka_time=fields.Integer(string=u'补卡次数')
    leave_1=fields.Float(string=u'平时请假（H）')
    leave_2=fields.Float(string=u'周末请假（H）')
    late_time_min=fields.Float(compute='compute_late_time_min')
    leave_early_min=fields.Float(compute='compute_leave_early_min')
    def compute_leave_early_min(self):
        for record in self:
            record.leave_early_min=record.leave_early_time*60

    def compute_late_time_min(self):
        for record in self:
            record.late_time_min=record.late_time*60

class hrattendancemonthreportwizard(models.TransientModel):
    _inherit='hr.attendance.month.report.wizard'

    def action_confirm(self):
        import calendar
        day_now=self.date_time.strftime('%Y-%m-%d')  # 给定日期
        day_begin = day_now[0:7] + '-01'  # 月初肯定是1号 所以 直接替换 就可以
        monthRange = calendar.monthrange(int(day_now[0:3]), int(day_now[5:7]))  # 得到本月的天数
        day_end = day_now[0:7] +'-' +str(monthRange[1])

        if self.department_id:
            employee_ids=self.env['hr.employee'].search([('department_id','=',self.department_id.id)])
            record_id=self.env['hr.attendance.wizard'].create({
                'start_date':day_begin,
                'end_date':day_end,
                'department_id':self.department_id.id
            })
        else:
            employee_ids=self.env['hr.employee'].search([])
            record_id=self.env['hr.attendance.wizard'].create({
                'start_date':day_begin,
                'end_date':day_end,
            })
        record_id.action_confirm()

        month_time=day_now[0:7]
        # if not self.department_id:
        #     employee_ids=self.env['hr.employee'].search([])
        # else:
        #     employee_ids=self.env['hr.employee'].search([('department_id','=',self.department_id.id)])
        #
        for employee_id in employee_ids:
            reports=self.env['hr.attendance.month.report']
            report_id=self.env['hr.attendance.month.report'].search([('employee_id','=',employee_id.id),('month_time','=',month_time)])
            if not report_id:
                self.env['hr.attendance.month.report'].create({
                    'employee_id':employee_id.id,
                    'department_id':employee_id.department_id.id,
                    'month_time':day_now[0:7],
                })
            overtime=employee_id.get_month_over_time(month_time)
            late_time=employee_id.get_month_late_time(month_time)
            absent_day=employee_id.get_month_absent_day(month_time)
            leave_early_time=employee_id.get_month_leave_early_time(month_time)
            leave_1,leave_2=employee_id.get_employee_monthly_leave(day_begin,day_end)
            buka_time=employee_id.get_buka_data(day_begin,day_end)
            total_day,attendance_1,attendance_2=employee_id.get_employee_monthly_attendance(day_begin,day_end)
            type1=self.env['hr.overtime.type'].search([('name','=','平时加班')])
            type2 = self.env['hr.overtime.type'].search([('name', '=', '节假日加班')])
            overtime_1=employee_id.get_employee_over_time_date_by_type(day_begin,day_end,type1)
            overtime_2 = employee_id.get_employee_over_time_date_by_type(day_begin, day_end,type2)
            report_id.write({
                'overtime':overtime,
                'absent_day':absent_day,
                'leave_early_time':leave_early_time,
                'leave_1':leave_1,
                'leave_2': leave_2,
                'total_day':total_day,
                'attendance_1':attendance_1,
                'attendance_2':attendance_2,
                'overtime_1': overtime_2,
                'overtime_1':overtime_1,
                'buka_time':buka_time,
                'late_time':late_time
            })
        reports|=report_id
        return {'type': 'ir.actions.act_window',
                'res_model': 'hr.attendance.month.report',
                'name': u'考勤月报表',
                'view_mode': 'tree',
                'domain': [('id', 'in', reports.ids)],
                'target': 'self',
                }
